package cn.itsource.crm.web.controller;

import cn.itsource.AjaxResult;
import cn.itsource.PageList;
import cn.itsource.crm.domain.Department;
import cn.itsource.crm.query.DepartmentQuery;
import cn.itsource.crm.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/department")
@Api(tags = "部门接口类")
@CrossOrigin
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;
    //分页查询方法
    @RequestMapping(value = "/queryPage",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=500,message="服务器错误")
    })
    @ApiOperation(value="分页查询部门",notes = "queryAll查询方法")
    @CrossOrigin
    public PageList queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    @RequestMapping(value = "/list",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiResponses({
         @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=500,message="服务器错误")
    })
    @ApiOperation(value="查询所有部门",notes = "queryAll查询方法")
    @CrossOrigin
    public List<Department> queryAll(){
        System.out.println("patch请求");
        return departmentService.queryAll();
    }

    // /department/1
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ApiOperation(value="根据ID查询用户",notes = "queryOne查询方法")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id",value = "id参数",required = true,dataType = "Long",paramType = "path")
//    })
    @ResponseBody
    public Department getOne(@PathVariable Long id){

        return  departmentService.queryOne(id);
    }


    @RequestMapping(value = "/save",method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value="保存部门方法",notes = "save方法")
    public AjaxResult save(@RequestBody  Department department){
        try {
            System.out.println("接收的department对象:");
            departmentService.save(department);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("保存失败");

        }
        return new AjaxResult();
    }
    //删除
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value="根据id删除部门方法",notes = "delete方法")
    public AjaxResult delete(@PathVariable Long id){
        try {
            System.out.println("删除的id是:"+id);
            departmentService.delete(id);

        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败");

        }
        return new AjaxResult();
    }

    //修改
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="修改部门方法",notes = "update方法")
    public AjaxResult update(@RequestBody  Department department){
        try {
            System.out.println("修的对象:"+department);
            System.out.println(department.getId());
            departmentService.update(department);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("修改失败");
        }
        return new AjaxResult();
    }
}
